﻿using EntitySpaces.Interfaces;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using yncTech.Common;

public partial class Admin_Hlp_Hlp001a : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (ActionType == OperationType.Delete)
            {
                btnSave_Click(null, null);
            }
        }
    }


    protected void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {
            lbMessage.Text = "";
            if (!fileUpload.HasFile)
            {
                lbMessage.Text = "请选择要上传的帮助文件";
                return;
            }
            if (txtFileName.Text.Trim().Length < 1)
            {
                txtFileName.Text = fileUpload.FileName.Replace(Path.GetExtension(fileUpload.FileName), "");
            }

            string fileName = Guid.NewGuid().ToString() + Path.GetExtension(fileUpload.FileName);
            Session["fileName"] = fileName;

            string file = Server.MapPath("/") + "UploadFile\\HelpFile\\" + fileName;
            //Session["file"] = file;
            if (File.Exists(file))
            {
                File.Delete(file);
            }
            fileUpload.SaveAs(file);

            txtFileName.Required = true;
            txtFileName.Enabled = true;
            btnSave.Enabled = true;
            lbMessage.Text = "文件上传成功!";
        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;

        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (ActionType == OperationType.Delete)
        {
            if (Request.QueryString[BFDocumentMetadata.ColumnNames.Id] != null)
            {
                BFDocument finUploadLog = new BFDocument();
                finUploadLog.LoadByPrimaryKey(decimal.Parse(Request.QueryString[BFDocumentMetadata.ColumnNames.Id]));

                string file = Server.MapPath("/") + "UploadFile\\HelpFile\\" + finUploadLog.FileName;
                //Session["file"] = file;
                if (File.Exists(file))
                {
                    File.Delete(file);
                }
                finUploadLog.MarkAsDeleted();
                finUploadLog.Save();
                WebHelper.GotoParent();
            }
        }
        else
        {
            lbMessage.Text = "";
            string fileName = Session["fileName"].ToString();

            string currentUser = LoginUserInfo.CurrentUser.LoginName;
            DateTime currentDT = DateTime.Now;
            DateTimeFormatInfo format = new DateTimeFormatInfo();


            string Msg = string.Empty;

            using (esTransactionScope scope = new esTransactionScope())
            {
                try
                {
                    BFDocument finUploadLog = new BFDocument();
                    finUploadLog.FileDisplayName = txtFileName.Text;
                    finUploadLog.FileName = fileName;
                    finUploadLog.CreatedBy = currentUser;
                    finUploadLog.CreatedDate = DateTime.Now;
                    finUploadLog.Save();
                    scope.Complete();
                    WebHelper.CloseDialog(true);
                }
                catch (Exception ex)
                {
                    lbMessage.Text = ex.Message;

                }
            }
        }

    }
    protected void btnClose_Click(object sender, EventArgs e)
    {
        WebHelper.CloseDialog(true);
    }
}